import { Switch, Redirect, Route } from 'react-router-dom'
import React from 'react'

const fn = (props) => {
    return(
        props.routes.map((item,index)=>{
            return(
                <Switch>
                    <Route key={index} path={item.path} render={props =>{
                        if(item.children){
                            fn(item.children)//递归一下
                        }
                        return <item.component {...props} />

                        // else{
                            // if (item.requireAuth) {
                            //     // 需要守卫   // true: cart false:   
                            //     return <RouterGuard {...item} {...props} />
                            //     // return <item.component {...props} />
                            // } else {
                            //     // 直接渲染
                            //     return <item.component {...props} />
                            // }
                        // }
                    }}>

                    </Route>
                </Switch>
            )
        })
    )

}

const RouterGuard = (props) => {
    const { component: Component, ...otherProps } = props;
    return(
        <Switch>
            <Route {...otherProps} render = { props => {
                return ( // true: cart组件 false: 
                localStorage.getItem('token') ?
                    <Component {...props} /> :
                    <Redirect to='/login' />
                )
            }} />
        </Switch>
    )
}

const RouterView = (props) => {
    return(   
      <div>
          
      </div>
    )
}

export default RouterView


